package com.zdj.jdbc;

import org.junit.Test;

import java.sql.*;

/**
 * @author zhangdj
 * @date 2020/01/10 16:36
 */
public class JdbcUtil {

    public static final String URL = "jdbc:mysql://localhost:3306/test";
    public static final String USERNAME = "root";
    public static final String PASSWORD = "12345678";

    private static Connection conn = null;

    static {
        //1.加载驱动程序
        //2. 获得数据库连接
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception{

        //3.操作数据库，实现增删改查
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM t_user");
        //如果有数据，rs.next()返回true
        while(rs.next()){
            System.out.println(rs.getString("user_name")+" 邮箱："+rs.getString("user_email"));
        }
    }

    @Test
    public void preparedStatement() throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement("select * from push_plan where name like \"%\"?\"%\"");
        preparedStatement.setString(1, "月");
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            System.out.println(resultSet.getString("id"));
        }
    }




}
